
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import { InputLinkNodeAny, InputLinkNodeBoolean, InputLinkNodeFlow, InputLinkNodeNumber, InputLinkNodeObject, ObjectMapInputLinkNode } from "@/js/components/InputLinkNode"

export class ObjectMap extends GraphNode{
    layout?: DefaultLayout
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView)
        this.layout = layout
        this.appendChild(layout.container)
        
        layout.main?.left.appendChild(
            new InputLinkNodeAny(this, this.graphView as GraphView, {
                name: "url",
                desc: 'url',
                model: "entry",
                placeholder: 'url'
            })
        ).appendChild(
            new InputLinkNodeAny(this, this.graphView as GraphView, {
                name: "method",
                desc: 'method',
                model: "entry",
                placeholder: 'method'
            })
        )
        .appendChild(
            new InputLinkNodeNumber(this, this.graphView as GraphView, {
                name: "timeout",
                desc: 'timeout',
                model: "entry",
                placeholder: 'timeout'
            })
        )
        .appendChild(
            new ObjectMapInputLinkNode(this, this.graphView as GraphView, {
                name: "headers",
                desc: 'headers',
                model: "entry",
                showInput: false,
                placeholder: 'headers'
            })
        )
    }
}

ObjectMap.parser =  ({node,toNode}: any)=>{
    
    const next = toNode(node.outputs.output[0], 'enter')
    return `console.log(${toNode(node.enters.target[0], 'output') || node.userData.enters.target});${next}`
}